package net.lico;

import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * 将键道可挂载智能平台的txt词库文件转成小牛txt词库文件
 */
public class JD2XN {

    /**
     * 小牛声母、韵母(全拼+简拼)
     */
    public static Map<String, String> shengmu = new HashMap<String, String>();
    public static Map<String, String> yunmu = new HashMap<String, String>();
    /**
     * 键道声母、韵母(全拼+简拼) —— 或声韵
     */
    public static Map<String, String> shengmuJD = new HashMap<String, String>();
    public static Map<String, String> yunmuJD = new HashMap<String, String>();
    /**
     * 小牛零声母
     */
    public static Map<String, String> lingXN = new HashMap<String, String>();
    /**
     * 键道零声母
     */
    public static Map<String, String> lingJD = new HashMap<String, String>();

    /**
     * 单字全拼编码 (中文字符，编码)
     */
    public static Map<String, Set<String>> danziQuanPinMap = new HashMap<String, Set<String>>();
    /**
     * 零声母汉字集合
     */
    public static Set<String> lingShengMuSet = new HashSet<String>();
    public static Set<String> emptyChineseSet = new HashSet<String>();

    /**
     * 组合声母(ZHI,CHI)
     */
    public static List<String> zuheSM = new ArrayList<String>();

    //------------- 过程中需要参考的文件：
    //键道手心的声母韵母映射配置文件
//    public static String palminput_cfg_jd = "G:\\soft\\viewer&editor\\input\\键道6挂载\\键道6挂载手心\\conf-phone-new.txt";
    public static String palminput_cfg_jd = "./resource/shuangPin-config/jd2Palm/conf-phone-new.txt";
    //小牛手心的声母韵母映射配置文件
//    public static String palminput_cfg_xn = "G:\\soft\\viewer&editor\\input\\一道\\一道挂载手心\\YD-conf-phone-new.txt";
    public static String palminput_cfg_xn = "./resource/shuangPin-config/xn2Palm/xn.ini";
    //单字全拼编码文件
//    public static String danziFile = "G:\\soft\\viewer&editor\\input\\全拼输入法汉字编码WINPY.TXT";
    public static String danziFile = "./resource/Pinyin/全拼输入法汉字编码WINPY.TXT";

    //键道词库文件多合一挂载文件
//    public static String jdFile = "D:\\Program Files\\Rime_JD\\rime\\dict-palminput-0708.txt";
        public static String jdFile = "./resource/jdDict/dict-palminput-20.0907.txt";
//        public static String jdFile = "D:\\Program Files\\Rime_JD\\rime\\dict-palminput-0811.txt";


    static {
        //小牛零声母 对应 键道的 v  不一样
        lingJD.put("xs", "a");
        lingJD.put("xh", "ai");
        lingJD.put("xf", "an");
        lingJD.put("xp", "ang");
        lingJD.put("xz", "ao");
        lingJD.put("xe", "e");
        lingJD.put("xw", "ei");
        lingJD.put("xn", "en");
        lingJD.put("xr", "eng");
        lingJD.put("xj", "er");
        lingJD.put("xl", "o");
        lingJD.put("xd", "ou");

        lingXN.put("aa", "a");
        lingXN.put("ai", "ai");
        lingXN.put("an", "an");
        lingXN.put("ag", "ang");
        lingXN.put("ao", "ao");
        lingXN.put("ee", "e");
        lingXN.put("ei", "ei");
        lingXN.put("en", "en");
        lingXN.put("eg", "eng");
        lingXN.put("er", "er");
        lingXN.put("oo", "o");
        lingXN.put("ou", "ou");
    }

    public static void main(String[] args) throws Exception {

        List<String> jd2XNList = convertJD2XN();

        //最终结果写入文件
        String dictFile = "./dict-20.09010.txt";

        try {
            List<String> newDictList = sortDict(jd2XNList);
            DictFileUtil.writeDictFile(dictFile, newDictList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("完事");

    }

    /**
     * 词库转换程序优化的点：
     * 	1.序号，如果一个词的序号不是1，并且当前词序前面的位置上没有其他的词。那么这个词的词序应该-1   ——递归
     * 	2.字形部分，如果一个字形减去一笔后不会跟其他的词重复。那么这一笔应该是需要去掉的 ——四码以上的词
     */
    public static List<String> sortDict(List<String> fileContentList){
        if(fileContentList == null)return null;
        Map<String, DictModel> dictMap = new LinkedHashMap<String, DictModel>();
        for (String s : fileContentList) {
            String[] split = s.split(",");
            String code = split[0];
            String[] split2 = split[1].split("=");
            DictModel dictModel = new DictModel();
            dictModel.setCode(code);
            dictModel.setChs(split2[1]);
            int order = Integer.parseInt(split2[0]);
            dictModel.setInteger(order);
            //当词组和单字编码有冲突时,优先保留词组(中文字符长的那个)
            if(dictMap.get(code+","+order) != null && dictMap.get(code+","+order).getChs().length() > dictModel.getChs().length()) {
            } else {
                dictMap.put(code + "," + order, dictModel);
            }
        }
        Iterator<Map.Entry<String, DictModel>> iterator = dictMap.entrySet().iterator();
        Map<String, DictModel>  newDictMap = new LinkedHashMap<>();
        while(iterator.hasNext()){
            Map.Entry<String, DictModel> entry = iterator.next();
            String key = entry.getKey();
            DictModel dm = entry.getValue();
            if(dm == null) continue;
            //处理序号 1.序号，如果一个词的序号不是1，并且当前词序前面的位置上没有其他的词。那么这个词的词序应该-1   ——递归
            boolean deleted = false;
            if(dm.getInteger() != 1){
                DictModel dmTmp = dictMap.get(dm.getCode() + "," + (dm.getInteger() - 1));
                DictModel dmTmp2 = newDictMap.get(dm.getCode() + "," + (dm.getInteger() - 1));
                if(dmTmp == null && dmTmp2 == null){
                    dm.setInteger(dm.getInteger()-1);
                    iterator.remove();
                    newDictMap.put(dm.getCode() + "," + dm.getInteger(), dm);
//                    dictMap.put(dm.getCode() + "," + dm.getInteger(), dm);
                    deleted = true;
                }
            }
            //2.字形部分，如果一个字形减去一笔后不会跟其他的词重复。那么这一笔应该是需要去掉的 ——四码以上的词
            if(dm.getCode().length() <= 4) continue;
            String newCode = dm.getCode().substring(0, dm.getCode().length()-1);
            if(dictMap.get(newCode+","+dm.getInteger()) == null && newDictMap.get(newCode+","+dm.getInteger()) == null){
                dm.setCode(newCode);
                if(!deleted) {//已经删除过就不用再删了,否则会报错
                    iterator.remove();
                }
//                dictMap.put(key, dm);
                newDictMap.put(key, dm);
            }
        }
        List<String> newDictList = new ArrayList<String>();
        appendMapValuesToList(dictMap, newDictList);
        appendMapValuesToList(newDictMap, newDictList);
        return newDictList;
    }
    public static void appendMapValuesToList(Map<String, DictModel> dictMap, List<String> resultDictList){
        if(dictMap == null || resultDictList == null)return;
        for (DictModel dm : dictMap.values()) {
            StringBuffer newLine = new StringBuffer(dm.getCode());
            newLine.append(",").append(dm.getInteger()).append("=").append(dm.getChs());
            resultDictList.add(newLine.toString());
        }
    }

    /**
     * 是否为正确的全拼
     * @param sm
     * @param ym
     * @param chinese
     * @return
     */
    public static boolean isCorrectQuanPin(String sm, String ym, String chinese) {
        Set<String> codeSet = danziQuanPinMap.get(chinese);
        if (codeSet == null) {
            emptyChineseSet.add(chinese);
            return false;
        }
        if ("v".equals(ym)) {
            if (codeSet.contains(sm + "v") || codeSet.contains(sm + "u")) {
                return true;
            }
        } else {
            return codeSet.contains(sm + ym);
        }
        return false;
    }

    /** 转换键道词库为小牛词库-start **/

    /**
     * 将键道词库转成小牛：
     * 双拼部分：1.零声母，键道零声母引导“x”；小牛零声母引导见配置 lingXN。
     * 2.一般双拼处理：将键道双拼中的声母和韵母分别转换成全拼，最后将转换后的全拼结果再转成小牛的双拼
     * 形码部分：小牛(预期转换后的)的形和键道的形除了代表“横”的部分不一样以外（可以直接替换掉），其他是一模一样的。
     *          预期为小牛转换后的横为e，键道的横为v     v与小牛的ZH 声母冲突，所以横移到e上面
     *
     * @return
     * @throws Exception
     */
    public static List<String> convertJD2XN() throws Exception {
        //键道声韵手心配置文件
        readMappingFile(palminput_cfg_jd, false);
        //小牛声韵手心配置文件
        readMappingFile(palminput_cfg_xn, true);

        //开始转换
        //键道词库
        List<String> fileContentList = DictFileUtil.readDictFile(jdFile);
        List<String> resultList = new ArrayList<String>();
        //把键道的双拼编码转换为全拼编码时参考的单字全拼码表
        List<String> danziList = DictFileUtil.readDictFile(danziFile);
        for (String lineTmp : danziList) {
            if ("".equals(lineTmp) || lineTmp.length() < 2) continue;
            String dzCode = lineTmp.substring(1).trim();
            String ch = lineTmp.substring(0, 1);
            if (dzCode.startsWith("#")) {
                lingShengMuSet.add(ch);
                continue;
            }
            //zh、ch 组合声母
            if (dzCode.startsWith("zh") || dzCode.startsWith("ch")) {
                zuheSM.add(ch);
            }
            Set<String> codeList = danziQuanPinMap.get(ch);
            if (codeList == null) {
                codeList = new HashSet<String>();
                danziQuanPinMap.put(ch, codeList);
            }
            if (dzCode.indexOf(" ") != -1) {
                String[] s = dzCode.split(" ");
                for (String s1 : s) {
                    codeList.add(s1);
                }
            } else {
                codeList.add(dzCode);
            }
        }
        Set<String> addedLine = new HashSet<String>();
        for (String line : fileContentList) {
            line = line.trim();
            String[] dictStr = line.split(",");
            String[] chineseStr = line.split("=");
            String code = dictStr[0];
            String chinese = chineseStr[1].trim();
            if(chinese.length()>2)continue;
            StringBuffer result = new StringBuffer();
            //后缀，除了声韵以外的“形”部分
            String subffix = "";
            //过滤纯形字词
            if (code.startsWith("a") || code.startsWith("v") ||
                    code.startsWith("u") || code.startsWith("i") || code.startsWith("o")) {
//                    System.out.println(line);
                continue;
            }
            if (code.length() >= 4) {
                String x = code.substring(2);
                //提前将键道的“横v”笔画改成一道的e
                subffix = code.substring(4).replace("v", "e");
                boolean isSingleCode = false;
                //判断是否为超过4码的单字（第三码包含形码）——低于四码的也是一样的处理
                if (x.startsWith("a") || x.startsWith("v") || x.startsWith("u") || x.startsWith("i") || x.startsWith("o")) {
                    isSingleCode = true;
                }
                if (chinese.length() == 0) {
                    continue;
                }
                if (chinese.length() == 1) {
                    isSingleCode = true;
                }
                //跳过三码的词组
                if (isSingleCode && chinese.length() > 1) {
                    continue;
                }
                if (isSingleCode) {
                    //提前将键道的“横v”笔画改成小牛的e
                    subffix = code.substring(2).replace("v", "e");
                    code = code.substring(0, 2);
                    //零声母提前转成小牛的
                    String ling = getXNLingShengMu(code, chinese);
                    if (StringUtils.isNotBlank(ling)) {
                        result.append(ling.substring(0, 1)).append(",").append(ling.substring(1, 2)).append(",").append(subffix);
                    } else {
                        convertToQuanPin(code, chinese, result, subffix);
                    }
                } else {//四码
                    String syTmp = code.substring(0, 4);
                    String firstCode = syTmp.substring(0, 2);
                    String secondCode = syTmp.substring(2, 4);
                    String chinese1 = null;
                    String chinese2 = null;
                    if (chinese.length() == 2) {
                        chinese1 = chinese.substring(0, 1);
                        chinese2 = chinese.substring(1, 2);
                    }
                    //零声母提前转成小牛的
                    String ling1 = getXNLingShengMu(firstCode, chinese1);
                    String ling2 = getXNLingShengMu(secondCode, chinese2);
                    if (StringUtils.isBlank(ling1)) {
                        convertToQuanPin(result, firstCode, chinese1);
                    } else {
                        //ling1
                        result.append(ling1.substring(0, 1)).append(",").append(ling1.substring(1, 2)).append(",");
                    }
                    if (StringUtils.isBlank(ling2)) {
                        convertToQuanPin(secondCode, chinese2, result, subffix);
                    } else {
                        //ling2
                        result.append(ling2.substring(0, 1)).append(",").append(ling2.substring(1, 2)).append(",").append(subffix);
                    }
                }

            } else if (code.length() > 1 && chinese.length() == 1) {//长度大于一，小于四
                subffix = code.substring(2).replace("v", "e");
                code = code.substring(0, 2);
                //零声母提前转成小牛的
                String ling = getXNLingShengMu(code, chinese);
                if (StringUtils.isNotBlank(ling)) {
                    result.append(ling.substring(0, 1)).append(",").append(ling.substring(1, 2)).append(",").append(subffix);
                } else {
                    convertToQuanPin(code, chinese, result, subffix);
                }
                if (StringUtils.isBlank(result.toString())) {
//                    System.out.println(line+" empty#");
                    continue;
                } else if (result.toString().contains("null")) {
//                    System.out.println(line);
                    continue;
                } else {
//                    System.out.println(result);
                }
            } else if (code.length() == 1) {
                //有部分键道的一简字，在小牛需要特殊处理
                StringBuffer tmpResult = new StringBuffer();
                convertToQuanPin(result, code, chinese);
//                String tmp = getKeyByJDMapValue(shengmuJD, code, null, chinese);
                String orderNum = line.substring(line.indexOf(",")+1,line.indexOf("="));
                if("1".equals(orderNum)){
                    System.out.print(line +" 293 "+ result+"    ");
                }
//                continue;
//                if (jdMap.values().contains(code)) {
//                
//                }
            } else {
                continue;
            }
            String quanPin = result.toString();
            if (StringUtils.isBlank(quanPin) ||//跳过纯形码的词 
                    quanPin.contains("null")) {//跳过键道中错误的词（会导致拼音中出现“null”）
//                System.out.println(line+" empty");
                continue;
            }
            //已经从键道词库转换好的小牛单字编码
//            Set<String> convertedDZCode = new HashSet<String>();
            if (StringUtils.isNotBlank(quanPin)) {
                String[] split = quanPin.split(",");
                StringBuffer xnResult = new StringBuffer();

                if (split.length == 1) {
                    String sm = split[0];
                    String codeNew = convertToXN(sm, null);
                    xnResult.append(codeNew);
                    xnResult.append(",");
                    xnResult.append(dictStr[1]);
                    System.out.println(xnResult);
                }else if (split.length == 2) {
                    String sm = split[0];
                    String ym = split[1];
                    String codeNew = convertToXN(sm, ym);
                    xnResult.append(codeNew);
                    xnResult.append(",");
                    xnResult.append(dictStr[1]);
                } else if (split.length == 3) {//单字
                    String sm = split[0];
                    String ym = split[1];
                    String codeNew = convertToXN(sm, ym);
                    xnResult.append(codeNew);
                    xnResult.append(split[2]);
                    xnResult.append(",");
                    xnResult.append(dictStr[1]);
                } else if (split.length == 5) {//词组（带形码的）
                    xnResult.append(convertToXN(split[0], split[1]));
                    xnResult.append(convertToXN(split[2], split[3]));
                    xnResult.append(split[4]);
                    xnResult.append(",");
                    xnResult.append(dictStr[1]);
                }
                if (split.length == 4) {//词组（不带形码的）
                    xnResult.append(convertToXN(split[0], split[1]));
                    xnResult.append(convertToXN(split[2], split[3]));
                    xnResult.append(",");
                    xnResult.append(dictStr[1]);
                }
                String xnStr = xnResult.toString().trim();
                if (StringUtils.isBlank(xnStr)) continue;
                //防止重复添加
                if (addedLine.contains(xnStr)) continue;
                addedLine.add(xnStr);
                resultList.add(xnStr);
//                System.out.println(xnResult.toString().trim());
            } else {

//                System.out.println(result + " " + line);
            }
        }
        //待补充在键道的一简“f”位置且被删掉的字

        System.out.println("=======================");
        for (String s : emptyChineseSet) {
            System.out.println(s +"   382");
        }
        return resultList;
    }

    /**
     * 将键道的拼音转成全拼
     *
     * @param result
     * @param firstCode
     * @param chinese
     */
    private static void convertToQuanPin(StringBuffer result, String firstCode, String chinese) {
        String sm = firstCode.substring(0, 1);
        String ym = null; if(firstCode.length() > 1){
            ym = firstCode.substring(1, 2);
        }
        //返回结果为null 则表明这个编码有误
        String codeNew = isFei(sm, ym, chinese);
        if(codeNew == null) {
            result.append("null").append(",").append("null").append(",");
            return;
        }
        String tmp = null;
        if (!sm.equals(codeNew)) {
            tmp = codeNew;
        } else {
            tmp = getKeyByJDMapValue(shengmuJD, sm, null, chinese);
        }
        if(ym == null){//韵母为空，直接返回声母
            result.append(tmp);
            return;
        }
        String tmp2 = getKeyByJDMapValue(yunmuJD, ym, tmp, chinese);
        if (isCorrectQuanPin(tmp, tmp2, chinese)) {
            result.append(tmp).append(",").append(tmp2).append(",");
        } else {
            result.append(tmp).append(",").append("null").append(",");
        }
    }

    /**
     * 将键道的拼音转成全拼（后面接形码）
     *
     * @param code
     * @param chinese
     * @param result
     * @param subffix 形码
     */
    private static void convertToQuanPin(String code, String chinese, StringBuffer result, String subffix) {
        String sm = code.substring(0, 1);
        String ym = code.substring(1, 2);
        //返回结果为null 则表明这个编码有误
        String codeNew = isFei(sm, ym, chinese);
        if(codeNew == null) {
            result.append("null").append(",").append("null").append(",").append(subffix);
            return;
        }
        String tmp = null;
        if (!sm.equals(codeNew)) {
            tmp = codeNew;
        } else {
            tmp = getKeyByJDMapValue(shengmuJD, sm, null, chinese);
        }
        String tmp2 = getKeyByJDMapValue(yunmuJD, ym, tmp, chinese);
        if (isCorrectQuanPin(tmp, tmp2, chinese)) {
            result.append(tmp).append(",").append(tmp2).append(",").append(subffix);
        } else {
            result.append(tmp).append(",").append("null").append(",").append(subffix);
        }
    }

    private static String convertToXN(String sm, String ym) {
        //已经提前转换好的零声母直接返回
        String ling = sm + ym;
        String tmp = lingXN.get(sm + ym);
        if (StringUtils.isNotBlank(tmp)) return ling;
        String s = shengmu.get(sm);
        if(ym == null) return s;
        String y = yunmu.get(ym);
        return s + y;
    }

    private static String getXNLingShengMu(String code, String chinese) {
        String ling = null;
        if (lingShengMuSet.contains(chinese)) {
            ling = lingJD.get(code);
            for (String xnCode : lingXN.keySet()) {
                String qpCode = lingXN.get(xnCode);
                if (StringUtils.equals(ling, qpCode)) {
                    ling = xnCode;
                    break;
                }
            }
        }
        return ling;
    }


    /**
     * 判断声母是否键道的飞键 —— 如果是的，那么返回的String将会和 firstCode不一样
     * 如果返回结果为null 则表明这个编码有误
     * @param firstCode  声母
     * @param secondCode 韵母
     * @param chinese
     * @return
     */
    private static String isFei(String firstCode, String secondCode, String chinese) {
        //飞键声母实际对应的那个全拼声母
        String newFirstCode = null;
        Set<String> codeSet = danziQuanPinMap.get(chinese);
        if (zuheSM.contains(chinese)) {
            //可能是飞键声母
            boolean flag = false;
            if ("q".equals(firstCode) || "j".equals(firstCode)) {//还会出现je 车 的飞键
                flag = true;
                if ("p".equals(secondCode) || "f".equals(secondCode) || "h".equals(secondCode) || "z".equals(secondCode) ||
                        "r".equals(secondCode) || "n".equals(secondCode) || "w".equals(secondCode) || "j".equals(secondCode)) {
                    if ("q".equals(firstCode)) {
//                        fei = true;
                        newFirstCode = "zh";
                    } else if ("j".equals(firstCode)) {
//                        fei = true;
                        newFirstCode = "ch";
                    }
                }

            } else if ("f".equals(firstCode) || "w".equals(firstCode)) {//wu fa
                flag = true;
                if ("f".equals(firstCode)) {
                    newFirstCode = "zh";
                } else if ("w".equals(firstCode)) {
                    newFirstCode = "ch";
                }
            }
            if(StringUtils.isNotBlank(newFirstCode)){
                //判断使用飞键的声母+韵母是否为这个汉字正确的读音，如果不是则说明这个字不是飞键
                if (secondCode == null && codeSet != null && codeSet.size() > 0) {//韵母为空时，只判断当前声母是否正确
                    for (String quanpin : codeSet) {
                        if(quanpin.startsWith(newFirstCode)){
                            return newFirstCode;
                        }
                    }
                }
                String newSecondCode = getKeyByJDMapValue(yunmuJD, secondCode, newFirstCode, chinese);
//                if (newFirstCode == null && !codeSet.contains(firstCode + newSecondCode)) {
                if (/*newFirstCode == null && */!isCorrectQuanPin(newFirstCode , newSecondCode, chinese)) {
//                emptyChineseSet.add(chinese);
                    String quanpin = shengmuJD.get(newFirstCode + secondCode);
                    //处理飞键（qwfj）的飞键
                    if (StringUtils.isNotBlank(quanpin) && (quanpin.startsWith("zh") || quanpin.startsWith("ch"))) {
                        return quanpin.substring(0, 2);
                    }
                    return null;
                } else {
                    return newFirstCode;
                }
            } else {
                if(flag) {
                    String tmpSM = getKeyByJDMapValue(shengmuJD, firstCode, null, chinese);
//                    System.out.println(tmpSM);
                    String newSecondCode = getKeyByJDMapValue(yunmuJD, secondCode, firstCode, chinese);
                    if (!isCorrectQuanPin(firstCode , newSecondCode, chinese)) {
                        return null;
                    }
                }
            }
        }
        //处理汉字的多音字情况且其中包含可飞键的声母（zh，ch）
        if (codeSet == null) {
            emptyChineseSet.add(chinese);
        }
        return firstCode;
    }

    /**
     * @param jdMap
     * @param code    需要处理的编码
     * @param shengmu 声母不为空，则表明当前处理的编码为韵母
     * @param chinese 中文字符
     * @return
     */
    public static String getKeyByJDMapValue(Map<String, String> jdMap, String code, String shengmu, String chinese) {
        Set<String> strings = jdMap.keySet();
        String result = null;
        boolean isRepeat = false;
        List<String> selected = new ArrayList<String>();
        String checkRepeat = null;
        for (String qpKey : strings) {
            String jp = jdMap.get(qpKey);
            if (jp != null && jp.equals(code)) {
                selected.add(qpKey);
                if (StringUtils.isNotBlank(checkRepeat)) {
                    isRepeat = true;
//                    break;
                } else {
                    checkRepeat = qpKey;
                }
            }
        }
        //出现重复的编码，从单字里面取编码
        if (isRepeat) {
            Set<String> codeSet = danziQuanPinMap.get(chinese);
            if (codeSet == null) {
                emptyChineseSet.add(chinese);
                return null;
            }
            for (String dzCode : codeSet) {
                if (StringUtils.isBlank(shengmu)) {//当前处理的就是声母
                    for (String s1 : selected) {
                        if (dzCode.startsWith(s1)) {
                            result = s1;
                            break;
                        }
                    }
                    if (StringUtils.isNotBlank(result)) break;//已经找到编码
                } else {//处理韵母
                    for (String s1 : selected) {
                        if (dzCode.startsWith(shengmu)) {
                            //处理v u的情况
                            if ("v".equals(s1) && (shengmu + "u").equals(dzCode)) {
                                result = s1;
                                break;
                            } else if ((shengmu + s1).equals(dzCode)) {
                                result = s1;
                                break;
                            }

                        }
                    }
                    if (StringUtils.isNotBlank(result)) break;//已经找到编码
                }
            }
        } else {
            result = checkRepeat;
        }
        return result;
    }

    /**
     * 初始化双拼方案声母、韵母的map
     *
     * @param filePath
     * @param isXN
     * @return
     * @throws Exception
     */
    public static void readMappingFile(String filePath, boolean isXN) throws Exception {
        List<String> dictFileContents = DictFileUtil.readDictFile(filePath);
        Map<String, String> tmpMap = null;
        //处理飞键的飞键。。 例如 je we 都表示车
        boolean jdShengyun = false;
        for (String line : dictFileContents) {
            if ("".equals(line)) continue;
            //小牛可以完全的把声母和韵母分离

            if ("[声母]".equals(line)) {
                if (isXN) {
                    tmpMap = shengmu;
                } else {
                    tmpMap = shengmuJD;
                }
            } else if ("[韵母]".equals(line)) {
                if (isXN) {
                    tmpMap = yunmu;
                } else {
                    tmpMap = yunmuJD;
                }
            } else if ("[声韵]".equals(line)) {
                if (isXN) {
                } else {
                    jdShengyun = true;
                    tmpMap = shengmuJD;
                }
            }
            if (line.indexOf("=") < 1) continue;
            if (line.startsWith("po=")) break;
            String quanPin = line.substring(0, line.indexOf("="));
//            if (codeMap.get(quanPin) == null) {
            String jianPin = line.substring(line.indexOf("=") + 1);
            if (tmpMap != null) {
                if (jdShengyun) {
                    //值和键调转过来——方便使用
                    tmpMap.put(jianPin, quanPin);
                } else {
                    tmpMap.put(quanPin, jianPin);
                }

            }
//            } else {
//                System.out.println(quanPin);
//            }
        }
    }
    /** 转换键道词库为小牛词库-end **/


    static class DictModel {
        private String chs;
        private String code;
        private int integer;

        public String getChs() {
            return chs;
        }

        public void setChs(String chs) {
            this.chs = chs;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public int getInteger() {
            return integer;
        }

        public void setInteger(int integer) {
            this.integer = integer;
        }

        @Override
        public String toString() {
            return "DictModel{" +
                    "chs='" + chs + '\'' +
                    ", code='" + code + '\'' +
                    ", integer=" + integer +
                    '}';
        }

    }

}
